package com.spring.cloud.study.user.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.spring.cloud.study.common.entity.SysRole;
import com.spring.cloud.study.user.dao.SysRoleDao;
import com.spring.cloud.study.user.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/2/24
 * Time: 13:28
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao,SysRole> implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public List<SysRole> findRoleByIds(List<Integer> ids) {
        return sysRoleDao.findRoleByIds(ids);
    }

    @Override
    public SysRole findRoleById(List<Integer> id) {
        return sysRoleDao.findRoleById(id);
    }

  /*  @Override
    public SysRole findByName(String name) {
        return null;
    }

    @Override
    public void saveRoleMenusById(int id, List<Integer> menuIds) {

    }*/
   /* @Autowired
    private SysRoleRepository sysRoleRepository;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public List<SysRole> findAll() {
        return sysRoleRepository.findAll();
    }

    @Override
    public void save(SysRole role) {
        if(role.getId() != null){
            SysRole sysRole = findById(role.getId());
            role.setMenus(sysRole.getMenus());
        }
        sysRoleRepository.save(role);
    }

    @Override
    public void saveRoleMenusById(int id, List<Integer> menuIds) {
        SysRole sysRole = findById(id);
        List<SysMenu> sysMenus = sysMenuService.findBatch(menuIds);
        sysRole.setMenus(new HashSet<>(sysMenus));
        sysRoleRepository.save(sysRole);
    }

    @Override
    public void saveBatch(Collection<SysRole> collection) {

    }

    @Override
    public SysRole findByName(String name) {
        return sysRoleRepository.findByName(name);
    }

    @Override
    @Transactional
    public Page<SysRole> findPage(Map<String, Object> params) {
        Specification specification =null;
        if(params.get("filter") != null && !params.get("filter").equals("")){
            String filter = params.get("filter").toString();
            JSONObject jsonObject = JSONObject.parseObject(filter);
            specification = new Specification<SysRole>() {
                @Override
                public Predicate toPredicate(Root<SysRole> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicates = new ArrayList<>();
                    if(jsonObject.get("name") != null && !jsonObject.get("name").equals("")){
                        predicates.add(criteriaBuilder.like(root.get("name"),"%"+jsonObject.get("name").toString()+"%"));
                    }
                    if(jsonObject.get("value") != null && !jsonObject.get("value").equals("")){
                        predicates.add(criteriaBuilder.like(root.get("value"),"%"+jsonObject.get("value").toString()+"%"));
                    }
                    return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            };
        }
        Sort sort = new Sort(Sort.Direction.ASC, "name");
        Pageable pageable = new PageRequest(Integer.parseInt(params.get("page").toString())-1, Integer.parseInt(params.get("limit").toString()), sort);
        Page<SysRole> page =null;
        if(specification != null){
            page = sysRoleRepository.findAll(specification,pageable);
        }else{
            page = sysRoleRepository.findAll(pageable);
        }
        return page;
    }

    @Override
    public List<SysRole> findBatch(Object[] objects) {
        return null;
    }

    @Override
    public List<SysRole> findBatch(Collection<Integer> collection) {
        List<SysRole> sysRoles = sysRoleRepository.findAll(collection);
        return sysRoles;
    }

    @Override
    public SysRole findById(int id) {
        return sysRoleRepository.findOne(id);
    }

    @Override
    public void update(SysRole sysRole) {

    }
    */

/*
    @Override
    public int delete(SysRole sysRole) {
        return 0;
    }

    @Override
    public void delete(int id) {
        sysRoleRepository.delete(id);
    }

    @Override
    public int deleteBatch(Object[] objects) {
        return 0;
    }

    @Override
    public void deleteBatch(Collection<Integer> ids) {
        ids.parallelStream().forEach(o->{
            sysRoleRepository.delete(o);

        });
    }*/

}
